package com.lightkits.wms.domain.deliveryorder.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lightkits.common.db.entity.FndResourceTl;
import com.lightkits.common.db.entity.SopDemand;
import com.lightkits.common.db.entity.syscode.SysCodeBase;
import com.lightkits.common.db.entity.syscode.SysCodeValueBase;
import com.lightkits.common.db.entity.syscode.SysCodeValueTl;
import com.lightkits.demand.dto.demandorder.SaveDoRequestType;
import com.lightkits.demand.dto.demandorder.SaveDoResponseType;
import com.lightkits.demand.dto.demandorder.model.DoServiceDTO;
import com.lightkits.demand.dto.dotrack.CreateDoTrackRequestType;
import com.lightkits.demand.dto.dotrack.CreateDoTrackResponseType;
import com.lightkits.demand.dto.dotrack.model.DoTrackServiceDTO;
import com.lightkits.framework.common.model.IResult;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.wms.client.DoServiceClient;
import com.lightkits.wms.client.DoTrackServiceClient;
import com.lightkits.wms.domain.deliveryorder.model.DloTrackModel;
import com.lightkits.wms.domain.deliveryorder.service.doDoubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * doDomainServiceImpl
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/3/13 15:54
 */
@Service
public class DoDoubboServiceImpl implements doDoubboService {

    @Autowired
    private DoServiceClient doService;

    @Autowired
    private DoTrackServiceClient doTrackService;

    /**
     * 改变需求订单状态
     *
     * @param doId     需求订单Id
     * @param status   当前状态
     * @param workerId 员工Id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public IResult updDoStatus(Long doId, String status, String workerId) {
        if (null == doId) {
            return IResult.fail("Update do status error.");
        }
        SaveDoRequestType doRequest = new SaveDoRequestType();
        List<DoServiceDTO> serviceDTOList = new ArrayList<>();
        DoServiceDTO doServiceDTO = new DoServiceDTO();
        doServiceDTO.setId(doId.toString());
        doServiceDTO.setDemandStatus(status);
        serviceDTOList.add(doServiceDTO);
        //改变需求订单状态
        doRequest.setDemandOrders(serviceDTOList);
        SaveDoResponseType response = doService.saveDo(doRequest);
        if (!response.isSuccessful()) {
            return IResult.fail(String.format("Call dubbo service 'DoService' error,msg: %s", response.getMessage()));
        }
        //创建需求i订单改变的订单追踪
        CreateDoTrackRequestType requestType = new CreateDoTrackRequestType();
        List<DoTrackServiceDTO> doTrackServiceDTOS = new ArrayList<>();
        DoTrackServiceDTO doTrackServiceDTO = new DoTrackServiceDTO();
        SopDemand sopDemand = new SopDemand()
                .selectById(doId);
        if (sopDemand != null) {
            doTrackServiceDTO.setOrganizationId(ObjectUtil.toString(sopDemand.getOrganizationId()));
            doTrackServiceDTO.setWorkerId(workerId);
            FndResourceTl resourceTl = new FndResourceTl().selectOne(new EntityWrapper().eq(FndResourceTl.RESOURCE_ID, workerId));
            doTrackServiceDTO.setWorkerName(resourceTl.getResourceName());
            SysCodeBase sysCodeBase = new SysCodeBase()
                    .selectOne(new EntityWrapper<SysCodeBase>()
                            .eq(SysCodeBase.CODE, "SOP_DEMAND_STATUS"));
            SysCodeValueBase sysCodeValueBase = new SysCodeValueBase()
                    .selectOne(new EntityWrapper<SysCodeValueBase>()
                            .eq(SysCodeValueBase.CODE_ID, sysCodeBase.getId())
                            .eq(SysCodeValueBase.CODE_VALUE, status));
            SysCodeValueTl codeValueTl = new SysCodeValueTl()
                    .selectOne(new EntityWrapper<SysCodeValueTl>()
                            .eq(SysCodeValueTl.CODE_VALUE_ID, sysCodeValueBase.getId()));
            if (codeValueTl == null) {
                return IResult.fail("SysCode not exist where code =" + status);
            }
            doTrackServiceDTO.setKeyValue(codeValueTl.getMeaning());
            doTrackServiceDTO.setActionRecord("订单已完成");
        }
        doTrackServiceDTO.setDemandId(doId.toString());
        doTrackServiceDTOS.add(doTrackServiceDTO);
        requestType.setDoTracks(doTrackServiceDTOS);
        CreateDoTrackResponseType createDoResponse = doTrackService.createDoTrack(requestType);
        if (!createDoResponse.isSuccessful()) {
            return IResult.fail(String.format("Call dubbo service 'DoTrackService' error,msg: %s", response.getMessage()));
        }
        return IResult.success();
    }

    /**
     * 创建需求订单追踪
     *
     * @param model
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public IResult createTrack(DloTrackModel model) {
        CreateDoTrackRequestType requestType = new CreateDoTrackRequestType();
        List<DoTrackServiceDTO> doTrackServiceDTOS = new ArrayList<>();
        DoTrackServiceDTO doTrackServiceDTO = new DoTrackServiceDTO();
        doTrackServiceDTO.setOrganizationId(model.getOrgId());
        doTrackServiceDTO.setWorkerId(model.getWorkerId());
        FndResourceTl resourceTl = new FndResourceTl().selectOne(new EntityWrapper().eq(FndResourceTl.RESOURCE_ID, model.getWorkerId()));
        doTrackServiceDTO.setWorkerName(resourceTl.getResourceName());
        doTrackServiceDTO.setKeyValue(model.getKeyValue());
        doTrackServiceDTO.setActionRecord("订单已发货数量" + model.getKeyValue());
        doTrackServiceDTO.setDemandId(model.getDemandId());
        doTrackServiceDTOS.add(doTrackServiceDTO);
        requestType.setDoTracks(doTrackServiceDTOS);
        CreateDoTrackResponseType createDoResponse = doTrackService.createDoTrack(requestType);
        if (!createDoResponse.isSuccessful()) {
            return IResult.fail(String.format("Call dubbo service 'DoTrackService' error,msg: %s", createDoResponse.getMessage()));
        }
        return IResult.success();

    }

}
